package com.zdsoft.finance.meet.service.process;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zdsoft.bpm.annotation.client.AutoJob;
import com.zdsoft.finance.meet.entity.MeetingProject;
import com.zdsoft.finance.meet.service.MeetingProjectService;
import com.zdsoft.finance.project.entity.Project;
import com.zdsoft.finance.project.service.ProjectService;
import com.zdsoft.finance.spi.meeting.dto.MeetingProjectStatus;
import com.zdsoft.framework.core.common.exception.AppException;
import com.zdsoft.framework.core.common.protocol.RPCRequest;
import com.zdsoft.framework.core.common.protocol.RPCResponse;
import com.zdsoft.framework.core.server.RPCAbstractService;
import com.zdsoft.framework.cra.annotation.InterfaceService;
import com.zdsoft.bpm.service.client.BPM;

@Service("meetingProjectConvert")
@AutoJob(label = "项目转化为上会项目", resource = "zf.autojob.business.meetingProjectConvert")
@InterfaceService(label = "项目转化为上会项目", resource = "zf.autojob.business.meetingProjectConvert")
public class MeetingProjectConvert extends RPCAbstractService {

	@Autowired
	ProjectService projectService;

	@Autowired
	MeetingProjectService meetingProjectService;

	@Autowired
	BPM BPM;

	@Override
	@Transactional
	public RPCResponse remoteCall(RPCRequest request) throws AppException {
		RPCResponse response = new RPCResponse();

		// 获取工作流参数
		Map<String, Object> params = request.getBodys();
		String projectId = (String) params.get("projectId");
		String businessId = (String) params.get("businessId");

		// 查询项目
		Project project = projectService.findById(projectId);

		// 组装MeetingProject
		MeetingProject meetingProject = new MeetingProject();
		meetingProject.setBusinessId(businessId);
		// 暂无
		// TODO .. 后续完善此操作
		meetingProject.setMinutesUrl("");
		meetingProject.setMinutesUrlAppCode("");
		meetingProject.setMinutesUrlukey("");
		meetingProject.setProMsgUrl("");
		meetingProject.setProUrlAppCode("");
		meetingProject.setProUrlukey("");
		meetingProject.setState(MeetingProjectStatus.MEET_PROJECT_STATE_WAIT.value);

		meetingProject.setProject(project);

		try {
			// 保存上会项目
			meetingProjectService.saveEntity(meetingProject);
			response.setStatus(RPCResponse.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			response.setStatus(RPCResponse.FAILURE);
		}

		return response;
	}

}
